package com.ruoyi.web.controller.talentServerController;

import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.enums.TalentInfoImportEnum;
import com.ruoyi.common.enums.TalentInfoStateEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TalChangeApply;
import com.ruoyi.system.domain.TalCuptureNotice;
import com.ruoyi.system.domain.TalPolicy;
import com.ruoyi.system.domain.dto.NoticeTalentInfo;
import com.ruoyi.system.domain.dto.NoticeTemplate;
import com.ruoyi.system.service.ITalChangeApplyService;
import com.ruoyi.system.service.ITalCuptureNoticeService;
import com.ruoyi.system.service.ITalPolicyService;
import com.ruoyi.system.service.impl.TalPolicyServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TalTalentInfo;
import com.ruoyi.system.service.ITalTalentInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人才信息Controller
 *
 * @author zhusq
 * @date 2023-03-27
 */
@RestController
@RequestMapping("/system/info")
public class TalTalentInfoController extends BaseController
{
    @Autowired
    private ITalTalentInfoService talTalentInfoService;

    @Autowired
    private ITalPolicyService policyService ;

    @Autowired
    private ITalChangeApplyService applyService ;

    @Autowired
    private ITalCuptureNoticeService cuptureNoticeService ;



    /**
     * 查询人才信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TalTalentInfo talTalentInfo)
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        for (SysRole role : roles) {
            if ("admin".equals(role.getRoleKey()) || "teacher".equals(role.getRoleKey())) {
                logger.info("管理员");
            }else {
                logger.info("普通用户");
                talTalentInfo.setUserId(userId);
            }
        }
        List<TalTalentInfo> list = talTalentInfoService.selectTalTalentInfoList(talTalentInfo);
        return getDataTable(list);
    }

    /**
     * 导出人才信息列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, TalTalentInfo talTalentInfo)
    {
        List<TalTalentInfo> list = talTalentInfoService.selectTalTalentInfoList(talTalentInfo);
        ExcelUtil<TalTalentInfo> util = new ExcelUtil<TalTalentInfo>(TalTalentInfo.class);
        util.exportExcel(response, list, "党员信息数据");
    }

    /**
     * 获取人才信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(talTalentInfoService.selectTalTalentInfoById(id));
    }

    /**
     * 新增人才信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody TalTalentInfo talTalentInfo)
    {
        LambdaQueryWrapper<TalTalentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true,TalTalentInfo::getUserId,SecurityUtils.getUserId()) ;
        TalTalentInfo one = this.talTalentInfoService.getOne(wrapper);
        if (!Objects.isNull(one)){
            throw new ServiceException("您已经申请过了，请勿重复申请！") ;
        }
        return toAjax(talTalentInfoService.insertTalTalentInfo(talTalentInfo));
    }

    /**
     * 修改人才信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody TalTalentInfo talTalentInfo)
    {
        return toAjax(talTalentInfoService.updateTalTalentInfo(talTalentInfo));
    }

    /**
     * 删除人才信息
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(talTalentInfoService.deleteTalTalentInfoByIds(ids));
    }

    /**工作人员审核*/
    @GetMapping("/talentProcess")
    public AjaxResult talentProcess(@RequestParam("id") Long id){
        this.talTalentInfoService.talentProcess(id) ;
        return success();
    }

    /**管理员审核*/
    @GetMapping("/adminProcess")
    public AjaxResult adminProcess(@RequestParam("id") Long id){
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            throw new ServiceException("当前账号没有审核权限！") ;
        }
        this.talTalentInfoService.adminProcess(id) ;
        return success();
    }

    /**取消申请*/
    @GetMapping("/processCancel")
    public AjaxResult processCancel(@RequestParam("id") Long id){
        LambdaUpdateWrapper<TalTalentInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TalTalentInfo::getId,id) ;
        wrapper.set(TalTalentInfo::getState, TalentInfoStateEnum.TALENT_CANCEL.getCode()) ;
        talTalentInfoService.update(wrapper) ;
        return success();
    }

    /**申请驳回*/
    @GetMapping("/processRollback")
    public AjaxResult processRollback(@RequestParam("id") Long id){
        LambdaUpdateWrapper<TalTalentInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TalTalentInfo::getId,id) ;
        wrapper.set(TalTalentInfo::getState, TalentInfoStateEnum.TALENT_ROLLBACK.getCode()) ;
        talTalentInfoService.update(wrapper) ;
        return success();
    }

    /**重新申请*/
    @GetMapping("/reprocess")
    public AjaxResult reprocess(@RequestParam("id") Long id){
        LambdaUpdateWrapper<TalTalentInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TalTalentInfo::getId,id) ;
        wrapper.set(TalTalentInfo::getState, TalentInfoStateEnum.TALENT_TOBE_TALENT_PROCESS.getCode()) ;
        talTalentInfoService.update(wrapper) ;
        return success();
    }

    @GetMapping("/getSelectOption")
    public AjaxResult getSelectOption(){
        List<TalPolicy> policies = policyService.list();
        List<TalChangeApply> applies = applyService.list();
        List<TalCuptureNotice> cuptureNotices = cuptureNoticeService.list();
        ArrayList<NoticeTemplate> templates = new ArrayList<>();

        for (TalPolicy policy : policies) {
            NoticeTemplate template = new NoticeTemplate();
            template.setId(UUID.randomUUID().toString());
            template.setTitle(policy.getTitle());
            template.setContent(policy.getContext());
            templates.add(template) ;
        }

        for (TalChangeApply apply : applies) {
            NoticeTemplate template = new NoticeTemplate();
            template.setId(UUID.randomUUID().toString());
            template.setTitle(apply.getTitle());
            template.setContent(apply.getContent());
            templates.add(template) ;
        }


        for (TalCuptureNotice cuptureNotice : cuptureNotices) {
            NoticeTemplate template = new NoticeTemplate();
            template.setId(UUID.randomUUID().toString());
            template.setTitle(cuptureNotice.getTitle());
            template.setContent(cuptureNotice.getContent());
            templates.add(template) ;
        }


        return success(templates) ;
    }

    @GetMapping("/getPartyList")
    public AjaxResult getPartyList(){
        LambdaQueryWrapper<TalTalentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TalTalentInfo::getImportState , TalentInfoImportEnum.TALENT_IMPORT.getCode()) ;
        List<TalTalentInfo> list = talTalentInfoService.list(wrapper);
        return success(list) ;
    }
}
